package com.lin.filegraph.utils.compdg;

import com.lin.filegraph.utils.filedg.CouplingType;

import java.util.Map;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 10:47
 */
public class ComponentEdge implements Comparable<ComponentEdge>{
    private ComponentNode preComponent;// 前节点
    private ComponentNode postComponent;// 后节点
    private boolean hasInherOrCombin;
    private double dependence;
    private int dependNum=1;
    private String couplingTypes;
    private boolean bringCouples=false;
    private boolean isCompile=false;
    public boolean isFromCompile() {
        return isCompile;
    }
    public void SetFromCompile(int iscompile) {
        isCompile=iscompile==1?true:false;
    }
    public int getCombinationCount() {
        Map<CouplingType, Integer> typemap=CouplingType.getMapByStr(couplingTypes);
        return typemap.get(CouplingType.Combination)!=null?typemap.get(CouplingType.Combination):0;
    }
    public int getOthersCount() {
        Map<CouplingType, Integer>typemap=CouplingType.getMapByStr(couplingTypes);
        int n1=0,n2=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0;
        if(typemap.get(CouplingType.Other_Association)!=null)
            n1=typemap.get(CouplingType.Other_Association);
        if(typemap.get(CouplingType.Call)!=null)
            n2=typemap.get(CouplingType.Call);
        if(typemap.get(CouplingType.Instantiation)!=null)
            n3=typemap.get(CouplingType.Instantiation);
        if(typemap.get(CouplingType.Parameter)!=null)
            n4=typemap.get(CouplingType.Parameter);
        if(typemap.get(CouplingType.Return)!=null)
            n5=typemap.get(CouplingType.Return);
        if(typemap.get(CouplingType.DeclarationType)!=null)
            n6=typemap.get(CouplingType.DeclarationType);
        if(typemap.get(CouplingType.Include)!=null)
            n7=typemap.get(CouplingType.Include);
        if(typemap.get(CouplingType.Import)!=null)
            n8=typemap.get(CouplingType.Import);
        return n1+n2+n3+n4+n5+n6+n7+n8;
    }
    public int getMainCount() {
        Map<CouplingType, Integer>typemap=CouplingType.getMapByStr(couplingTypes);
        int n1=0,n2=0,n3=0;
        if(typemap.get(CouplingType.Inheritance)!=null)
            n1=typemap.get(CouplingType.Inheritance);
        if(typemap.get(CouplingType.Implements)!=null)
            n2=typemap.get(CouplingType.Implements);
        if(typemap.get(CouplingType.DeclareToDefine)!=null)
            n3=typemap.get(CouplingType.DeclareToDefine);
        return n1+n2+n3;
    }
    /**
     * 前节点+后节点+继承+依赖强度+目录强度+依赖类型
     */
    public ComponentEdge(ComponentNode preComponent, ComponentNode postComponent, boolean hasInherOrCombin, double dependence, String couplingTypes) {
        super();
        this.preComponent = preComponent;
        this.postComponent = postComponent;
        this.hasInherOrCombin = hasInherOrCombin;
        this.dependence = dependence;
        this.couplingTypes = couplingTypes;
    }

    public ComponentEdge(ComponentNode preComponent, ComponentNode postComponent, boolean hasInheritance, double dependence) {
        this(preComponent, postComponent,hasInheritance,dependence,null);
    }

    public ComponentEdge(ComponentNode preComponent, ComponentNode postComponent) {
        this(preComponent,postComponent,false,0.0);
    }

    public ComponentNode getPreComponent() {
        return preComponent;
    }

    public ComponentNode getPostComponent() {
        return postComponent;
    }

    public void setPreComponent(ComponentNode preComponent) {
        this.preComponent = preComponent;
    }

    public void setPostComponent(ComponentNode postComponent) {
        this.postComponent = postComponent;
    }

    @Override
    public String toString() {
        return preComponent.getStrComponentName() + " -> " + postComponent.getStrComponentName() +"   "+ dependence;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof ComponentEdge)) {
            return false;
        } else {
            ComponentEdge edge2 = (ComponentEdge) obj;
            if (this.getPreComponent().equals(edge2.getPreComponent())
                    && this.getPostComponent().equals(edge2.getPostComponent())) {
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + preComponent.hashCode();
        result = 31 * result + postComponent.hashCode();
        return result;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(ComponentEdge o) {
        // TODO Auto-generated method stub
        int preCompare = this.getPreComponent().getStrComponentName()
                .compareTo(o.getPreComponent().getStrComponentName());
        if (preCompare != 0) {
            return preCompare;
        } else {
            return this.getPostComponent().getStrComponentName().compareTo(o.getPostComponent().getStrComponentName());
        }

    }

    /**
     * @return the dependence
     */
    public double getDependence() {
        return dependence;
    }

    /**
     * @param dependence the dependence to set
     */
    public void setDependence(double dependence) {
        this.dependence = dependence;
    }
    public boolean isHasInherOrCombin() {
        return hasInherOrCombin;
    }
    public void setHasInherOrCombin(boolean hasInheritance) {
        this.hasInherOrCombin = hasInheritance;
    }

    public String getCouplingTypes() {
        return couplingTypes;
    }

    public void setCouplingTypes(String couplingTypes) {
        this.couplingTypes = couplingTypes;
    }

    public boolean isBringCouples() {
        return bringCouples;
    }

    public void setBringCouples(boolean bringCouples) {
        this.bringCouples = bringCouples;
    }

    public int getDependNum() {
        return dependNum;
    }

    public void setDependNum(int dependNum) {
        this.dependNum = dependNum;
    }

}

